<?php

class AdvertisingController extends AdminBase{
	public function actionCreate()
	{
		$this->baseCreate('AdvertisingModel');
	}
	
	public function actionUpdate($id)
	{
		$objModel = $this->loadModel($id);
		$objModel->setScenario('admin');       
		if( isset( $_POST['AdvertisingModel'] ) )
		{	
			//赋值
			$arrOld = $objModel->attributes;
			$objModel->attributes = $_POST['AdvertisingModel'];
		
			if($objModel->save())
			{
				//修改成功后要看图片有没有修改，如果有修改的话需要删除原来的,
				if($arrOld['Image']  &&  $objModel->Image != $arrOld['Image']){
					UploadFile::deleteImg($arrOld['Image']);
				}			
				$this->success('成功修改');
			}else
			{
				$error = $objModel->getOneError();
				$this->error($error ? $error : '修改失败！');				
			}
		}
		$this->render('update',array('model' => $objModel));
	}



	public function actionIndex()
	{
		$this->baseIndex('AdvertisingModel');
	}


	public function actionDelete($id)
	{
		$this->loadModel($id)->delete();
		$this->success('删除成功');		
	}

	public function loadModel($id)
	{
		$model=AdvertisingModel::model()->findByPk($id);
		if($model===null)
			$this->error('找不到记录');
		return $model;
	}
}